package com.house365.core.activity;

import com.baidu.mobstat.StatService;
import com.house365.app.analyse.HouseAnalyse;
import com.house365.app.analyse.data.AnalyseMetaData;
import com.house365.core.R;
import com.house365.core.action.ActionTag;
import com.house365.core.application.BaseApplication;
import com.house365.core.constant.CorePreferences;
import com.house365.core.image.AsyncImageLoader;
import com.house365.core.image.CacheImageUtil;
import com.house365.core.image.CacheImageUtil.TextImagePosition;
import com.house365.core.touch.ImageViewTouch;
import com.house365.core.view.LoadingDialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.DialogInterface.OnCancelListener;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 *@author py
 *@date 2012-11-26
 *@comment
 */
public abstract class BaseCommonActivityWithFragment extends FragmentActivity {

	protected String getTAG() {
		return this.getClass().getName();

	}
	protected Activity thisInstance; 
	protected LoadingDialog tloadingDialog ;
	private AsyncImageLoader mAil;
	protected BaseApplication mApplication;
	protected AlertDialog.Builder alertDialog;
	private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			finish();
		}
	};

	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		thisInstance = this;
		mApplication = (BaseApplication) getApplication();
		registerReceiver(mLoggedOutReceiver, new IntentFilter(
				ActionTag.INTENT_ACTION_LOGGED_OUT));
		
		preparedCreate(savedInstanceState);
		initView();
		initData();
	}

	protected abstract void preparedCreate(android.os.Bundle savedInstanceState);
	protected abstract void initView();
	protected abstract void initData();

	protected void onDestroy() {
		super.onDestroy();
		dismissLoadingDialog();
		tloadingDialog = null;
		alertDialog = null;
		if(mAil!= null){
			mAil.clearCacheImage();
		}
		clean();
//		mApplication.onAppCancel();
		unregisterReceiver(mLoggedOutReceiver);
	}

	public void setImage(final ImageView imageView, String imageUrl,
			final int resId,int scaleType) {
		CacheImageUtil.setCacheImage(imageView, imageUrl, resId, scaleType, getImageLoader());
	}

	public void setTextImage(final TextView textView,TextImagePosition position, String imageUrl) {
		CacheImageUtil.setCacheTextImage(textView,position,imageUrl, getImageLoader());
	}
	public void setTouchImage(final ImageViewTouch imageView, String imageUrl, final int resId,int scaleType) {
		CacheImageUtil.setTouchImage(imageView, imageUrl,getResources(),resId, scaleType, getImageLoader());
	}
	
	public AsyncImageLoader getImageLoader() {
		if (mAil == null) {
			mAil = new AsyncImageLoader(this);
		}
		return mAil;
	}
	private LoadingDialog getLoadingDialog(){
		if(tloadingDialog == null){
			tloadingDialog= new LoadingDialog(this,
					R.style.dialog, R.string.loading);
			tloadingDialog.setCancelable(isCancelDialog());
			if(isCancelDialog()){
				tloadingDialog.setOnCancelListener(new OnCancelListener() {
					
					@Override
					public void onCancel(DialogInterface dialog) {
						onExitDialog();
					}
				});
			}
		
		}
		return tloadingDialog;
	}
	 
	public void showLoadingDialog(){
		if(getLoadingDialog() != null){
			getLoadingDialog().show();
		}
	}
	public void showLoadingDialog(int resid){
		if(getLoadingDialog() != null){
			getLoadingDialog().setMessage(this.getResources().getString(resid));
			getLoadingDialog().show();
		}
	}
	public void dismissLoadingDialog(){
		if(getLoadingDialog() != null){
			getLoadingDialog().dismiss();
		}
	}
	public AlertDialog.Builder getAlertDialog(){
		if(alertDialog == null){
			alertDialog = new AlertDialog.Builder(this);
		}
		return alertDialog;
	}
	
	public void showToast(String str){
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}
	public void showToast(int resId){
		Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
	}
	public void onExitDialog(){
		thisInstance.finish();
	}
	public boolean isCancelDialog(){
		return true;
	}
	
	protected  void clean(){
		
	}
	AnalyseMetaData analyseMetadata ;
	protected void onResume() {
		super.onResume();
		if(CorePreferences.getInstance(this).getCoreConfig().isAnalyse()){
			analyseMetadata = HouseAnalyse.onPageResume(this);
		}
		if(CorePreferences.getInstance(this).getCoreConfig().isOpenBaiduStat()){
			StatService.onResume(this);
		}
	}
	
	protected void onPause() {
		super.onPause();
		if(CorePreferences.getInstance(this).getCoreConfig().isAnalyse()){
			HouseAnalyse.onPagePause(analyseMetadata);	
		}
		if(CorePreferences.getInstance(this).getCoreConfig().isOpenBaiduStat()){
			StatService.onPause(this);
		}
		
	}


}
